//
//  EvaluateViewController.m
//  ForestPack
//
//  Created by 郑洲 on 2018/7/19.
//  Copyright © 2018年 郑洲. All rights reserved.
//

#import "EvaluateViewController.h"
#import "PaySuccessViewController.h"

@interface EvaluateViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) BaseTableView *tableView;

@end

@implementation EvaluateViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"估价";
    if (_isDesign) {
        self.title = @"设计订单";
    }
    
    self.tableView = [[BaseTableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - NAVIGATION_BAR_HEIGHT) style:UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = [UIColor whiteColor];
    self.tableView.rowHeight = ScreenX375(44);
    [self.view addSubview:self.tableView];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (_dict) {
        return 7;
    }
    return 8;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellId = @"detailCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
        
        NSArray *noteArr = @[@"印刷方式：",@"箱型结构：",@"材       质：",@"表面处理：",@"成箱方式",@"打包方式：",@"规       格：",@"数       量："];
        NSArray *detailArr = @[@"水印",@"四半摇盖",@"A4F-E",@"过油",@"粘箱",@"绳捆",@"100*100*100",@"100"];
        if (_dict) {
            noteArr = @[@"产       品：",@"长         ：",@"宽         ：",@"高         ：",@"重       量：",@"层       数：",@"设计需求："];
            detailArr = @[@"goods_name",@"long",@"wide",@"height",@"weight",@"stack",@"demand"];
        }
        UILabel *noteLabel = [[UILabel alloc] initWithFrame:CGRectMake(ScreenX375(13), 0, ScreenX375(75), ScreenX375(44))];
        noteLabel.text = noteArr[indexPath.row];
        noteLabel.textColor = REXADECIMALCOLOR(@"#333333");
        noteLabel.font = Kfont(14);
        [cell addSubview:noteLabel];
        
        UILabel *detailLabel = [[UILabel alloc] initWithFrame:CGRectMake(ScreenX375(87), 0, ScreenX375(175), ScreenX375(44))];
        detailLabel.text = detailArr[indexPath.row];
        if (_dict) {
            detailLabel.text = [_dict valueForKey:detailArr[indexPath.row]];
        }
        detailLabel.textColor = REXADECIMALCOLOR(@"#999999");
        detailLabel.font = Kfont(14);
        [cell addSubview:detailLabel];
        
        UIImageView *line = [[UIImageView alloc] initWithFrame:CGRectMake(ScreenX375(13), ScreenX375(44) - 0.5, SCREEN_WIDTH - ScreenX375(26), 0.5)];
        line.image = [UIImage imageNamed:@"虚线"];
        [cell addSubview:line];
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.01;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return ScreenX375(224);
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, ScreenX375(224))];
    
    UILabel *valueLabel = [[UILabel alloc] initWithFrame:CGRectMake(ScreenX375(13), 0, SCREEN_WIDTH - ScreenX375(26), ScreenX375(45))];
    valueLabel.textColor = ThemeColor;
    valueLabel.font = Kfont(16);
    valueLabel.textAlignment = NSTextAlignmentRight;
    if (!_isDesign) {
        [view addSubview:valueLabel];
    }
    
    NSString *string = @"价格：¥10000.00";
    NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc]initWithString:string];
    [attributedStr addAttribute:NSForegroundColorAttributeName value:REXADECIMALCOLOR(@"#333333") range:NSMakeRange(0, 3)];
    valueLabel.attributedText = attributedStr;
    
    UIButton *createBtn = [[UIButton alloc] initWithFrame:CGRectMake(ScreenX375(13), ScreenX375(90), ScreenX375(166), ScreenX375(44))];
    [createBtn setTitle:@"生成支付链接" forState:UIControlStateNormal];
    [createBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    createBtn.titleLabel.font = Kfont(16);
    createBtn.backgroundColor = REXADECIMALCOLOR(@"#ff9500");
    createBtn.layer.cornerRadius = ScreenX375(22);
    createBtn.clipsToBounds = YES;
    if (!_isDesign) {
        [view addSubview:createBtn];
    }
    
    UIButton *buyBtn = [[UIButton alloc] initWithFrame:CGRectMake(ScreenX375(196), ScreenX375(90), ScreenX375(166), ScreenX375(44))];
    if (_isDesign) {
        buyBtn.frame = CGRectMake(ScreenX375(13), ScreenX375(90), SCREEN_WIDTH - ScreenX375(26), ScreenX375(44));
    }
    [buyBtn setTitle:@"立即购买" forState:UIControlStateNormal];
    [buyBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    buyBtn.titleLabel.font = Kfont(16);
    buyBtn.backgroundColor = ThemeColor;
    buyBtn.layer.cornerRadius = ScreenX375(22);
    buyBtn.clipsToBounds = YES;
    [buyBtn addTarget:self action:@selector(buy) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:buyBtn];
    
    return view;
}

- (void)buy {
    if (_dict) {
        NSMutableDictionary *parameter = [NSMutableDictionary dictionaryWithDictionary:_dict];
        [parameter setObject:[FPLoginAndRegisterTools getToken] forKey:@"token"];
        if (_pic) {
            [HttpManager uploadImageArr:[NSMutableArray arrayWithObject:self.pic] success:^(id successResponse) {
                NSString *pic = successResponse[0];
                [parameter setObject:pic forKey:@"pics"];
                
                [HttpManager postHttpRequestByPost:[NSString stringWithFormat:@"%@app/personal_apply_order",REQUESTHEADER] andParameter:parameter success:^(id successResponse) {
                    NSString *status = [NSString stringWithFormat:@"%@",successResponse[@"status"]];
                    if ([status integerValue] == 1) {
                        [HttpManager showNoteMsg:@"购买成功"];
                        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                            PaySuccessViewController *vc = [[PaySuccessViewController alloc] init];
                            [self.navigationController pushViewController:vc animated:YES];
                        });
                    }
                } andFailure:^(id failureResponse) {
                    [HttpManager showFail];
                }];
            }];
        }else {
            [HttpManager postHttpRequestByPost:[NSString stringWithFormat:@"%@app/personal_apply_order",REQUESTHEADER] andParameter:parameter success:^(id successResponse) {
                NSString *status = [NSString stringWithFormat:@"%@",successResponse[@"status"]];
                if ([status integerValue] == 1) {
                    [HttpManager showNoteMsg:@"购买成功"];
                    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                        PaySuccessViewController *vc = [[PaySuccessViewController alloc] init];
                        [self.navigationController pushViewController:vc animated:YES];
                    });
                }
            } andFailure:^(id failureResponse) {
                [HttpManager showFail];
            }];
        }
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
